﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.Utils;
using Tailord;
using TailordDS;
using Extensions;

namespace ConfExpert.Utility
{
    public class RepositoryManager
    {
        public RepositoryManager()
        {
            try
            {  
                //Initialize();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw ex;
            }
        } 
 
        public static AppDS dsi = new AppDS();

        static Dictionary<int, RepositoryItem> repositories = new Dictionary<int, RepositoryItem>();

        public static RepositoryItem Get(int LookUpId)
        {
            RepositoryItem ri;
            repositories.TryGetValue(LookUpId, out ri);
            return ri;
        }

        public static RepositoryItem Get(Rp repository)
        {
            return repositories[(int)repository];
        }      

        

        public static RepositoryItemPictureEdit GetPhotoRI()
        {
            RepositoryItemPictureEdit riPhoto = new RepositoryItemPictureEdit();
            riPhoto.SizeMode = PictureSizeMode.Zoom;
            riPhoto.NullText = " ";
            riPhoto.Padding = new Padding(-1);
            return riPhoto;
        }

      

        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            //gleSablon.PopupFilterMode = AppSession.Settings.PopupFilterMode;
            //search columns via the View's OptionsFind.FindFilterColumns property. In the "StartsWith" or "Contains" mode, the search is performed against a field specified by the RepositoryItemSearchLookUpEdit.DisplayMember property.

            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            //gleSablon.View.OptionsView.EnableAppearanceEvenRow = AppSession.Settings.EnableAppearanceEvenRow;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.TextEditStyle = TextEditStyles.Standard; // DisableTextEditor;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;

                if (gc.FieldName == "PhotoMikro")
                {
                    RepositoryItemPictureEdit glePic = GetPhotoRI();

                    gleSablon.View.RowHeight = 32;
                    gleSablon.View.Columns["PhotoMikro"].Width = 32;

                    gc.ColumnEdit = glePic;
                    gc.Caption = "P";
                }
            }

            return gleSablon;
        }

     


        
        public static RepositoryItemGridLookUpEdit riState
        {
            get
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("StateId",typeof(char));
                dt.Columns.Add("State", typeof(string));
                dt.Rows.Add('N', "New");
                dt.Rows.Add('O', "Waiting for production");
                dt.Rows.Add('P', "In Production");
                dt.Rows.Add('S', "Sent");
                RepositoryItemGridLookUpEdit ri = GetGLESablon(dt, "State", "StateId");
                return ri;
            }
        }

       
      


        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

        void gridview_RowStyle(object sender, RowStyleEventArgs e)
        {
            object deletedValue = (((GridView)sender).GetRowCellValue(e.RowHandle, "Deleted"));
            if (deletedValue != null && deletedValue != DBNull.Value && (bool)deletedValue)
                e.Appearance.ForeColor = Color.LightGray;                   
        }
    }
}